<?php

class PlayerTable extends Doctrine_Table
{
    public function getPlayerList()
    {
        $q = $this->createQuery("player")
            ->select("*");
        return $q;
    }
    public function getGuildedPlayerList()
    {
        $q = $this->createQuery("player")
            ->select("*")
            ->where("guildedflag=?", 1);
        return $q;
    }

    public function getBackendList(Doctrine_Query $q)
    {
        $rootAlias = $q->getRootAlias();
        $q->leftJoin($rootAlias.".PClass c")
          ->leftJoin($rootAlias.".Gender g")
          ->leftJoin($rootAlias.".Race race")
          ->leftJoin($rootAlias.".Rank rank")
          //->leftJoin($rootAlias.".PlayerTalent pt WITH pt.active=1")
          ->leftJoin($rootAlias.".sfGuardUser o");
        return $q;
    }

    public function getPlayerListByRank()
    {
        $q = $this->createQuery('p')
            ->innerJoin("p.Rank r")
            ->orderBy("r.id");
        $r = $q->execute();

        $oldRank = "";
        $return = array();
        foreach ($r as $player)
        {
            if ($player->getRank()->getName() != $oldRank)
            {
                $oldRank = $player->getRank()->getName();
                $return[$oldRank] = array();
            }
            $parray = $player->toArray();
            $return[$oldRank][] = $parray["fullname"];
        }
        return $return;
    }

    public function ajaxAutocompleter($value)
    {
        $q = $this->createQuery('p')
                 ->where("lower(p.name) like lower(?)", '%'.$value.'%');
        $r = $q->execute();
        $json = array();
        foreach ($r as $data)
        {
            $json[$data->getId()] = $data->getFullname();
        }
        return $json;
    }
}